// Copyright(c) 2008 Tri Tech Information Systems Inc. 
// Distributed under the Boost Software License, Version 1.0.
//     (See accompanying file ../../LICENSE_1_0.txt or copy at
//           http://www.boost.org/LICENSE_1_0.txt)
//     
#include <llvm/AbstractTypeUser.h>
#include <boost/python.hpp>
#include <pyxx/pointer.h>


template< typename TypeType >
struct TypePointer : public llvm::PATypeHolder
{
    typedef TypeType element_type;
    typedef TypePointer<element_type> this_type;

    TypePointer(element_type * type) :
        llvm::PATypeHolder(type)
    {}

    static void install()
    {
        add_pointer_handler<TypeType>( &this_type::from_ptr );
    }

    static boost::python::object from_ptr(const void * vptr)
    {
        using namespace boost::python;
        void * fvptr = const_cast<void*>(vptr);
        return object( this_type( reinterpret_cast<element_type*>(fvptr) ) );
    }
};

template<typename TypeType>
TypeType * get_pointer(const TypePointer<TypeType> & ptr)
{
    return (TypeType*)ptr.get();
}


namespace boost
{
    namespace python
    {
        template<typename TypeType>
        struct pointee< TypePointer<TypeType> >
        {
            typedef TypeType type;
        };
        
    }
}
